package com.bluesky.plum.uimodels.standard.components.objectBrowser;

import java.util.List;

import com.bluesky.plum.domain.repository.Repository;
import com.bluesky.plum.domain.repository.RepositoryWrapper;
import com.bluesky.plum.richdomain.RichDomainField;
import com.bluesky.plum.uimodels.standard.FieldUIComponent;
import com.bluesky.plum.uimodels.standard.StandardUIComponentType;
import com.bluesky.plum.uimodels.standard.UIComponent;
import com.bluesky.plum.uimodels.standard.UIComponentBuilder;
import com.bluesky.plum.uimodels.standard.components.Button;
import com.bluesky.plum.uimodels.standard.components.EventListener;
import com.bluesky.plum.uimodels.standard.components.containers.Panel;
import com.bluesky.plum.uimodels.standard.components.containers.layout.VerticalBox;
import com.bluesky.plum.uimodels.standard.components.containers.window.Dialog;
import com.bluesky.plum.uimodels.standard.components.containers.window.Window;
import com.bluesky.plum.uimodels.standard.components.listbox.SelectionMode;
import com.bluesky.plum.uimodels.standard.components.listbox.Selector;
import com.bluesky.plum.uimodels.standard.components.objectBrowser.collectionEditor.CollectionEditDialogBuilder;

/*
 * it is as simple as a list box
 */
public class ObjectCollectionEditor<T> extends FieldUIComponent {

	protected Panel box;
	protected Button btn;
	// protected List objects;

	protected Repository<T> repo;

	public ObjectCollectionEditor(RichDomainField field) {
		super(field);

		// objects = (List)field.value;
	}

	@Override
	public void setUiComponentBuilder(UIComponentBuilder uiComponentBuilder) {

		super.setUiComponentBuilder(uiComponentBuilder);

		final Selector<T> selector;
		final Button addBtn;// =>saveBtn
		final Button removeBtn;
		final Button modifyBtn;// =>updateBtn

		box = (Panel) uiComponentBuilder
				.createNonFieldComponent(StandardUIComponentType.Panel);
		box.setName("object-collection-editor-box");
		
		btn = (Button) uiComponentBuilder
				.createNonFieldComponent(StandardUIComponentType.Button);

		btn.setCaption("...");

		// pop up a dialog of collection editor
		btn.setClickListener(new EventListener() {

			@Override
			public void process() {
				System.out.println("getComponentByClass() in collection editor");
				final Window window = getWindow();

				CollectionEditDialogBuilder dialogBuilder = new CollectionEditDialogBuilder(
						field, getUiComponentBuilder());
				Dialog dialog = dialogBuilder.getDialog(window,
						"collection edit dialog");

				window.getRootWindow().repaint();

				dialog.setModal(true);
				dialog.pack();
				dialog.setVisible(true);
				
				//remove it
				window.removeChildWindow(dialog);
				
				System.out.println("exit from collection edit dialog");
			}
		});

		setDomainFieldValue(field.value);

		
		box.addChildComponent(btn);
		
		addChildComponent(box);
	}


	@Override
	public Object getDomainFieldValue() {

		return field.value;

	}

	@Override
	public void setDomainFieldValue(Object value) {
		field.value = value;

	}

	@Override
	public Object getNativeComponent() {

		return ((UIComponent) box).getNativeComponent();
	}

}
